package com.checkoutcheckins.client.callbacks;

import com.checkoutcheckins.client.data.DataContainer;
import com.checkoutcheckins.client.rpc.CheckinResponse;
import com.checkoutcheckins.client.services.FoursquareServiceAsync;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class FoursquareCheckinsCallback implements
		AsyncCallback<CheckinResponse> {

	private DataContainer dataContainer = null;

	private FoursquareServiceAsync fsSvc = null;

	public FoursquareCheckinsCallback(FoursquareServiceAsync fsSvc,
			DataContainer dataContainer) {
		this.dataContainer = dataContainer;
		this.fsSvc = fsSvc;
	}

	public void onFailure(Throwable caught) {
		dataContainer.getStatusPanel().showErrorMessage(caught.getMessage(),
				false);
	}

	@Override
	public void onSuccess(CheckinResponse result) {

		dataContainer.getFoursquareCheckins().addCheckins(
				result.getCheckinList());
		dataContainer.updateFoursquareCheckins();

		System.out.println(result.getMessage());
		System.out.println("retrieved size: " + result.getCheckinList().size());

		if (!result.isFinished()) {
			dataContainer.getStatusPanel().showLoadMessage(result.getMessage(),
					true);
			dataContainer.getStatusPanel().showLoadMessage(
					"Getting some more checkins", false);
			fsSvc.getCheckinsFromFoursquare(result.getFoursquareId(), this);
		} else {
			dataContainer.getStatusPanel().showLoadMessage(
					"Retrieved all recent checkins", true);
			
			dataContainer.getStatusPanel().showLoadMessage("Getting friends checkins", false);

			fsSvc.getFriendsCheckinsFromFoursquare(result.getFoursquareId(),
					new FoursquareFriendsCheckinsCallback(fsSvc, dataContainer));
		}

	}

}
